package com.ruoyi.web.controller.shop.goods;

import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.shop.goods.domain.ShopGoodsModel;
import com.ruoyi.shop.goods.service.IShopGoodsModelService;
import com.ruoyi.shop.goods.vo.ShopGoodsModelVo;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 商品模型Controller
 * 
 * |----------------------------------------------------------------------|
 * |favorshop [简单好用，让您喜爱的小商城]
 * |----------------------------------------------------------------------|
 * |copyright www.favorshop.cn All rights reserved.
 * |----------------------------------------------------------------------|
 * | @author xuxiang
 * |----------------------------------------------------------------------|
 * | @date 2023-05-29
 * |----------------------------------------------------------------------|
 */
@Controller
@RequestMapping("/shop/goods/model")
public class ShopGoodsModelController extends BaseController {
	private String prefix = "shop/goods/model";

	@Autowired
	private IShopGoodsModelService shopGoodsModelService;

	@RequiresPermissions("shop:model:view")
	@GetMapping()
	public String model() {
		return prefix + "/model";
	}

	/**
	 * 查询商品模型列表
	 */
	@RequiresPermissions("shop:model:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(ShopGoodsModel goodsModel) {
		startPage();
		List<ShopGoodsModel> list = shopGoodsModelService.selectModelList(goodsModel);
		return getDataTable(list);
	}

	/**
	 * 新增模型
	 */
	@GetMapping("/add")
	public String add() {
		return prefix + "/add";
	}

	/**
	 * 新增保存模型
	 */
	@RequiresPermissions("shop:model:add")
	@Log(title = "模型", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(ShopGoodsModelVo goodsModelVo) {
		goodsModelVo.setUpdateBy(getLoginName());
		return toAjax(shopGoodsModelService.insertGoodsModel(goodsModelVo));
	}

	/**
	 * 修改商品模型
	 */
	@RequiresPermissions("shop:model:edit")
	@GetMapping("/edit/{modelId}")
	public String edit(@PathVariable("modelId") Long modelId, ModelMap mmap) {
		ShopGoodsModel goodsModel = shopGoodsModelService.selectModelById(modelId);
		mmap.put("goodsModel", goodsModel);
		return prefix + "/edit";
	}

	/**
	 * 修改保存商品模型
	 */
	@RequiresPermissions("shop:model:edit")
	@Log(title = "修改商品模型", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(ShopGoodsModel goodsModel) {
		return toAjax(shopGoodsModelService.updateGoodsModel(goodsModel));
	}

	/**
	 * 修改规格
	 */
	@GetMapping("/editSpec/{modelId}")
	public String editSpec(@PathVariable("modelId") Long modelId, ModelMap mmap) {
		ShopGoodsModel goodsModel = shopGoodsModelService.selectModelById(modelId);
		JSONArray array = JSONUtil.parseArray(goodsModel.getSpecValue());
		JSONArray specArray = JSONUtil.createArray();
		for (int i = 0; i < array.size(); i++) {
			JSONObject specObject = JSONUtil.createObj();
			specObject.set("specId", i);
			specObject.set("key", array.getJSONObject(i).get("key"));
			specObject.set("values", array.getJSONObject(i).get("values"));
			specObject.set("sort", array.getJSONObject(i).get("sort"));
			specArray.add(specObject);
		}
		mmap.put("modelId", modelId);
		mmap.put("specJson", specArray.toString());
		return prefix + "/editSpec";
	}

	/**
	 * 修改保存规格
	 */
	@RequiresPermissions("shop:model:edit")
	@Log(title = "修改规格信息", businessType = BusinessType.UPDATE)
	@PostMapping("/editSpec")
	@ResponseBody
	public AjaxResult editSpec(ShopGoodsModelVo goodsModelVo) {
		return toAjax(shopGoodsModelService.updateGoodsSpec(goodsModelVo));
	}

	/**
	 * 删除商品模型
	 */
	@RequiresPermissions("shop:model:remove")
	@Log(title = "商品模型", businessType = BusinessType.DELETE)
	@PostMapping("/remove")
	@ResponseBody
	public AjaxResult remove(String ids) {
		return toAjax(shopGoodsModelService.deleteModelByIds(ids));
	}
	
	/**
	 * 查询商品模型
	 * 
	 * @param modelId
	 * @param mmap
	 * @return
	 */
	@GetMapping("/selectModelById/{modelId}")
	@ResponseBody
	public AjaxResult selectModelById(@PathVariable("modelId") Long modelId, ModelMap mmap) {
		ShopGoodsModel goodsModel = shopGoodsModelService.selectModelById(modelId);
		return success(goodsModel);
	}
	
}
